use mylib::ThreadPool;
use std::fs;
use std::io::*;
use std::net::{TcpListener, TcpStream};
use std::{thread, time};
fn handle_client(mut stream: TcpStream) {
    let mut buffer = [0; 512];
    stream.read(&mut buffer).unwrap();
    println!("收到了{}", String::from_utf8_lossy(&buffer[..]));

    let get = b"GET / HTTP/1.1\r\n";
    let (status_line, filename) = if buffer.starts_with(get) {
        ("HTTP/1.1 200 OK \r\n\r\n", "main.html")
    } else {
        ("HTTP/1.1 404 NOT FIND \r\n\r\n", "404.html")
    };
    let content = fs::read_to_string(filename).unwrap();
    let reponse = format!("{}{}", status_line, content);
    stream.write(reponse.as_bytes()).unwrap();
    stream.flush().unwrap();

    //休眠
    let ten_millis = time::Duration::from_millis(10000);
    thread::sleep(ten_millis);
}

fn main() -> std::io::Result<()> {
    let listener = TcpListener::bind("127.0.0.1:9090")?;
    //let mut thread_vec: Vec<thread::JoinHandle<()>> = Vec::new();
    let pool = ThreadPool::new(4);
    for stream in listener.incoming() {
        // handle_client(stream?);
        let stream = stream.unwrap();
        // let handle = thread::spawn(move || {
        //     handle_client(stream);
        // });
        // thread_vec.push(handle)

        //thread pool
        pool.excute(|| handle_client(stream));
    }

    // for handle in thread_vec {
    //     handle.join().unwrap();
    // }

    Ok(())
}
